package com.coustemview.qianfeng.huaqiangbeistore.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.coustemview.qianfeng.huaqiangbeistore.R;
import com.coustemview.qianfeng.huaqiangbeistore.model.Shopcar_list_handler;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by mingma on 2016/9/2.
 */
public class Shopcar_listview_adapter extends BaseAdapter {
    private List<Shopcar_list_handler>data;
    private Context context;
    private CompoundButton.OnCheckedChangeListener mChangeListener;
    private View.OnClickListener mClickListener;
    public Shopcar_listview_adapter(List<Shopcar_list_handler> data, Context context,CompoundButton.OnCheckedChangeListener mChangeListener,View.OnClickListener mClickListener) {
        this.data = data;
        this.context = context;
//        mChangeListener=(CompoundButton.OnCheckedChangeListener)context;
//        mClickListener=(View.OnClickListener)context;
        this.mClickListener=mClickListener;
        this.mChangeListener=mChangeListener;
    }

    @Override
    public int getCount() {
        return data==null?0:data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
           ViewHolder holder=null;
           if(convertView==null) {
             convertView= LayoutInflater.from(context).inflate(R.layout.shopcar_listview_item,null);
             holder=new ViewHolder(convertView);
               convertView.setTag(holder);
           }else {
             holder= (ViewHolder) convertView.getTag();
           }
        Shopcar_list_handler listHandler = data.get(position);
        Picasso.with(context).load(listHandler.photo).into(holder.image_shopcar_item);
        holder.texttitle_shopcar_item.setText(listHandler.shoptitle);
        holder.textprice_shopcar_item.setText(listHandler.price);
        holder.textoldprice_shopcar_item.setText(listHandler.oldprice);
        holder.textnumber_shopcar_item.setText(1+"");

        //给checkbox设置选中的监听事件
         holder.check_shopcar_item.setOnCheckedChangeListener(mChangeListener);
          holder.check_shopcar_item.setTag(position);
         holder.check_shopcar_item.setChecked(data.get(position).ischecked);

          //给button添加监听事件
        holder.buttonadd_shopcar_item.setOnClickListener(mClickListener);
         holder.buttondeline_shopcar_item.setOnClickListener(mClickListener);
        holder.buttonadd_shopcar_item.setTag(position);
        holder.buttondeline_shopcar_item.setTag(position);

      //给删除添加监听事件
        holder.buttondelete_shopcar_item.setOnClickListener(mClickListener);
        holder.buttondelete_shopcar_item.setTag(position);

        return convertView;
    }
    static class ViewHolder{
        private CheckBox check_shopcar_item;
        private ImageView image_shopcar_item;
        private TextView texttitle_shopcar_item,textnumber_shopcar_item,textprice_shopcar_item,textoldprice_shopcar_item;
        private Button buttondeline_shopcar_item,buttonadd_shopcar_item,buttondelete_shopcar_item;

        public ViewHolder(View view) {
           check_shopcar_item= (CheckBox) view.findViewById(R.id.checkbox_shopcar_item);
            image_shopcar_item= (ImageView) view.findViewById(R.id.image_shopcar_item);
            textnumber_shopcar_item= (TextView) view.findViewById(R.id.textnumber_shopcar_item);
            textoldprice_shopcar_item= (TextView) view.findViewById(R.id.textoldprice_shopcar_item);
            textprice_shopcar_item= (TextView) view.findViewById(R.id.textprice_shopcar_item);
            texttitle_shopcar_item= (TextView) view.findViewById(R.id.texttitle_shopcar_item);
            buttonadd_shopcar_item= (Button) view.findViewById(R.id.buttonadd_shopcar_item);
            buttondelete_shopcar_item= (Button) view.findViewById(R.id.buttondelete_shopcar_item);
            buttondeline_shopcar_item= (Button) view.findViewById(R.id.buttondecline_shopcar_item);
        }
    }

    public int getMoney(){
        int money = 0;
        if (data==null||data.size()==0){
            return  money;
        }
        for (int i = 0; i < data.size(); i++) {
            Shopcar_list_handler listHandler = data.get(i);
                if(listHandler.ischecked) {
                    String price = listHandler.price;
                     int priceint = Integer.parseInt(price);
                     int number = priceint * listHandler.count;
                    money+= number;
                }
        }
        return money;
    }
}
